/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Stack;

import org.apache.log4j.Logger;

import who.tmap.ui.util.UserHomeHelper;

public class RecentFilesHelper {
	
    private static Logger logger = Logger.getLogger(RecentFilesHelper.class);
	
	private static final String RECENT_FILES_FILENAME ="tmapRecentFiles.properties";
	private static final int NB_FILES = 4;
	
	private Stack<String> recentFiles = new Stack<String>();
	
	private static RecentFilesHelper instance =null;
	
	public static synchronized RecentFilesHelper getInstance() {
		if ( instance == null ) {
			instance =new RecentFilesHelper();
		}
		
		return instance;
	}
	
	private RecentFilesHelper() {
		try {
			loadProperties( new UserHomeHelper().getTMapDir() + '/' + RECENT_FILES_FILENAME );
		} catch (Exception e){
			logger.error( e );
		}
	}
	
	/**
	 * Load recent files from properties file
	 */
	private void loadProperties( String propertiesFilename ) throws Exception {
		
		Properties properties =new Properties();
		properties.load( new FileInputStream( propertiesFilename ) );
		
		for ( int index = Math.min( properties.size(), NB_FILES ); index > 0; index-- ) {
			recentFiles.push( (String)properties.getProperty( "File" + index ) );
		}
		
	}
	
	/**
	 * Add a file to the list of files.<br>
	 * This method is called every time a project is opened or saved
	 * @param fileName
	 */
	public void addFile(String filePath) {
		if ( recentFiles.size() == 0 || !filePath.equals( recentFiles.peek() ) ) {
			logger.info( "push " + filePath );
			
			recentFiles.remove(filePath);
			
			recentFiles.push(filePath);
		}
	}	
	
	/**
	 * Save all recent files to the properties file
	 */
	public void saveFiles() throws IOException {

		Properties prop =new Properties();

		for( int index =recentFiles.size(); index > Math.max( recentFiles.size() - NB_FILES, 0 ); index-- ) {
			prop.put( "File" + (recentFiles.size() - index + 1), new File( recentFiles.elementAt(index-1) ).getAbsolutePath() );
		}
		
		prop.store( new FileOutputStream( new UserHomeHelper().getTMapDir() + '/' + RECENT_FILES_FILENAME ), null);
		
	}
	
	/**
	 * return the recent files for display purpose
	 * @return 
	 */
	public List<String> getFiles() {
		
		List<String> returnList = new ArrayList<String>();

		if ( recentFiles.size() > 0 ) {
			for( int index =recentFiles.size(); index > Math.max( recentFiles.size() - NB_FILES, 0); index-- ) {
				returnList.add( recentFiles.elementAt(index-1) );
			}
		}
		
		return returnList;
	}
	
}
